package Greedy;

import Tree.TreeNode;

public class 监控二叉树 {

    private int ans = 0;

    public int minCameraCover(TreeNode root) {
        if (postorder(root) == 0) ans++;
        return ans;
    }

    private int postorder(TreeNode root) {
        if (root == null) return 1;
        int left = postorder(root.left);
        int right = postorder(root.right);
        if (left == 0 || right == 0) {
            ans++;
            return 2;
        }
        if (left == 1 && right == 1) return 0;
        return 1;
    }
}
